import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store/index'

Vue.use(VueRouter)

function fn(next,msg) {
  let urls = store.state.user.menus_url
  if (urls.includes(msg)) {
    next()
    return
  } else {
    next('/')
  }
}


export const twopath = [
  {
    path: 'banner',
    meta: {
      title: '轮播图管理'
    },
    beforeEnter: (to, from, next) => {
      fn(next,'/banner')
    },
    component: () => import('../page/banner/banner.vue')
  },
  {
    path: 'cate',
    meta: {
      title: '商品分类'
    },
    beforeEnter: (to, from, next) => {
      fn(next,'/cate')
    },
    component: () => import('../page/cate/cate.vue')
  },
  {
    path: 'goods',
    meta: {
      title: '商品管理'
    },
    beforeEnter: (to, from, next) => {
      fn(next,'/goods')
    },
    component: () => import('../page/goods/goods.vue')
  },
  {
    path: 'manage',
    meta: {
      title: '管理员管理'
    },
    beforeEnter: (to, from, next) => {
      fn(next,'/manage')
    },
    component: () => import('../page/manage/manage.vue')
  },
  {
    path: 'member',
    meta: {
      title: '会员管理'
    },
    beforeEnter: (to, from, next) => {
      fn(next,'/member')
    },
    component: () => import('../page/member/member.vue')
  },
  {
    path: 'menu',
    meta: {
      title: '菜单管理'
    },
    beforeEnter: (to, from, next) => {
      fn(next,'/menu')
    },
    component: () => import('../page/menu/menu.vue')
  },
  {
    path: 'role',
    meta: {
      title: '角色管理'
    },
    beforeEnter: (to, from, next) => {
      fn(next,'/role')
    },
    component: () => import('../page/role/role.vue')
  },
  {
    path: 'seckill',
    meta: {
      title: '秒杀活动'
    },
    beforeEnter: (to, from, next) => {
      fn(next,'/seckill')
    },
    component: () => import('../page/seckill/seckill.vue')
  },
  {
    path: 'specs',
    meta: {
      title: '商品规格'
    },
    beforeEnter: (to, from, next) => {
      fn(next,'/specs')
    },
    component: () => import('../page/specs/specs.vue')
  },
]


const routes = [
  {
    path: '/',
    component: () => import('../page/index/index.vue'),
    children: [
      {
        path: '',
        meta: {
          title: ''
        },
        component: () => import('../page/home/home.vue'),

      },
      ...twopath,
      {
        path: '',
        redirect: ''
      }
    ]
  },
  {
    path: '/login',
    component: () => import('../page/login/login.vue')
  }, {
    path: '*',
    redirect: '/'
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path == '/login') {
    next()
    return
  }

  if (store.state.user.token) {
    next()
    return
  }
  console.log(from);
  next('/login')
})

export default router
